import type { SwiperComponent } from './interface'

/**
 * 将扁平数组数据转换成多个Swiper组件需要的数据
 * @param target 源数组
 * @param count 每个Swiper组件需要数据量
 * @returns
 */
export const transformArrayToSwiperComponentData = <T extends object>(
    target: T[],
    count: number = 3,
): T[][] => {
    const data = [...target]
    let res = []
    let i = 0,
        multipleIndex = data.length / count
    while (i <= multipleIndex) {
        let currentItem = data.splice(0, count)
        currentItem.length && res.push(currentItem)
        i++
    }
    return res
}

/** 生成SwiperPages */
export const generateSwiperPages = (
    originData: Record<string, any>[],
    component: (props?: any) => JSX.Element,
    /** 每页展示长度 */
    count?: number,
) => {
    const swiperData = transformArrayToSwiperComponentData(originData, count)
    const swiperComponents: SwiperComponent[] = swiperData.map((item, index) => ({
        component: component,
        isShow: true,
        props: {
            data: item,
            multipleIndex: index,
        },
    }))
    return swiperComponents
}
